const { findOne } = require('../models/users')

const subscribe = async (req, res, next) => {
    const { fans, blogger } = req.body
    res.set('content-type', 'application/json; charset=utf-8')
    const f = await findOne(fans), b = await findOne(blogger)
    if(f && b) {
        b.fans = b.fans.concat([{
            _id: f._id,
            username: f.username
        }])
        f.subs = f.subs.concat([{
            _id: b._id,
            username: b.username
        }])
        b.save()
        f.save()
        res.render('succ', {
            data: JSON.stringify('订阅成功')
        })
    } else {
        res.render('fail', {
            data: JSON.stringify('订阅失败')
        })
    }
}

module.exports = {
    subscribe,

}